package beyjava.view;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.Vector;

import beyjava.model.Activity;
import beyjava.model.Project;
import beyjava.util.Common;
import beyjava.util.IdGenerator;
import beyjava.util.Validator;

/**
 * @author BEYJAVA
 * activity view class
 */
public class ActivityMenu {
     private Scanner sc = null;
     /**
     * constructor of activityMenu class 
     */
     public ActivityMenu(){
            sc = new Scanner(System.in);
     }
     /**
     * @return integer choose of user
     * method for main activity menu
     */
     public int chooseMainMenu(){// maindeki show menu;
    		System.out.println("Please Choose A System Job");
    		System.out.println("1- Add Activity");
    		System.out.println("2- Remove Activity");
    		System.out.println("3- Update Activity");
    		System.out.println("0- Return Previous Menu");//Bu project menuye doncek
    	    return Common.getChoose(0, 3);
      }
    /**
     * @param currentProject
     * @return activity to controller class to add operation
     * @throws IllegalArgumentException
     * @throws InputMismatchException
     * method to take activity properties from user to add operation
     */
    public Activity addActivityMenu(Project currentProject){
  		     Activity addedActivity = null;
  		     Integer activityNumber;
  			 String activityDesc;
  			 String activityDeliver;
  			 Date activityStartDate;
  			 try{
	  			 System.out.println("Please Fill the Required Field For Adding An Activity.");
	  			 System.out.println("Activity Description :");
	  			 activityDesc = sc.next();
	  			 Validator.textHasContent(activityDesc);
	  			 activityNumber=IdGenerator.generateIntId();
	  			 System.out.println("Activity Deliver :");
	  			 activityDeliver = sc.next();
	  			 Validator.textHasContent(activityDeliver); 			 
	  			 System.out.println("Activity Start Date(Date format must be in this format dd-MM-yyyy) :");
	  			 activityStartDate  = Common.getDate("dd-MM-yyyy");
	  			 if(Common.dateComprasion(currentProject.getProjectStartDate(),activityStartDate)){
					 System.out.println("the activity start date  should be bigger than  project start date");
					 return null;
				 }
	  			 addedActivity = new Activity(activityNumber,activityDesc,activityStartDate,activityDeliver,0,currentProject);
  			 }
  			 catch(IllegalArgumentException ex){
  				System.out.println(ex.getMessage());
  			 }catch (InputMismatchException e){
				System.out.println("Input must be integer number.Please enter again.");
			 }
  			 return addedActivity;
  	  }
     /**
      * @param activities
      * @return selected activity to remove operation from user
      * method to list available activities and take choose of user
      */
      public  Activity removeActivityMenu(Vector<Activity> activities){
        	if(activities == null || activities.size() < 1){
    			System.out.println("There is no activity in system.");
    			return null;
    		}
    	    return selectActivityMenu(activities,"remove");
      }
     /**
      * @param activities
      * @return selected activity to update operation from user
      * method to list available activities and take choose of user
      */
      public Activity updateActivityMenu(Vector<Activity> activities){
	      	if(activities == null || activities.size() < 1){
	    			System.out.println("There is no activity in system. Please add an activity to system.");
	    			return null;
	    	}
	      	return selectActivityMenu(activities,"update");
      }
      /**
       * @param updatedActivity
       * @return choose of user to take which property of activity is updated.
       * method to list available operation to user and take choose of user
       */
      public int selectActivityUpdateAction(Activity updatedActivity){
    	    String DATE_FORMAT = "dd-MM-yyyy";
      	    SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT);
    	    System.out.println("Please choose one of to activity's information to update.");
 			System.out.println("1- Activity Description : " + updatedActivity.getActDesc());
 			System.out.println("2- Activity StartDate : " + sdf.format(updatedActivity.getActStartDate()));
 			System.out.println("3- Activity Deliverable : "+ updatedActivity.getActDeliver());
 			System.out.println("4- Please enter 4 for task operations. " );
 			System.out.println("0- Return Previous Menu."); //bu activity menuye doncek
 			return Common.getChoose(0, 4);
      }
      /**
       * @param updatedActivity
       * @param act
       * @return newActivity which contains updated property of updatedActivity
       * @throws Exception
       * method to take new Activity property from user to update operation
       */
      public Activity updatedActivityForOneToFour(Activity updatedActivity, int act){
	    	 Activity newActivity = new Activity(updatedActivity.getActNumber(), updatedActivity.getActDesc(),
	    			 updatedActivity.getActStartDate(), updatedActivity.getActDeliver(),updatedActivity.getHours(),
	    			 updatedActivity.getActivityProject());
	 		 String y="";
	 		 try{
	 				switch(act){
	 					case 1: System.out.println("Please enter new Activity Description :");
	 						y = sc.next();
	 						Validator.textHasContent(y);
	 						newActivity.setActDesc(y);
	 						break;
	 					case 2: System.out.println("Please enter new Activity StartDate :");
	 						
	 						Date date = Common.getDate("dd-MM-yyyy");
	 						 if(Common.dateComprasion(updatedActivity.getActivityProject().getProjectStartDate(),date)){
	 							 System.out.println("the activity start date  should be bigger than  project start date");
	 							 return null;
	 						 }
	 						newActivity.setActStartDate(date);
	 						break;
	 					case 3: System.out.println("Please enter new Activity Deliverable :");
	 						y = sc.next();
	 						Validator.textHasContent(y);
	 						newActivity.setActDeliver(y);
	 						break;
	 					default:
	 						break;
	 				}
	 		}
	 		catch(Exception ex){
	 			ex.getMessage();
	 		}
			return newActivity;
 	 	}
      /**
       * @param activities
       * @param action
       * @return activity which is chosen from user
       * method to list available activities for choose of user
       */
      public Activity selectActivityMenu(Vector<Activity> activities,String action){
     	    System.out.println("Your activities:");
    		for(int i = 0;i < activities.size();i++){
    			System.out.println((i+1)+"-"+activities.get(i).getActDesc().toUpperCase());
    		}
    		System.out.println("Please Write One Of The Activities' Number To "+action+":");
    	    int choose = Common.getChoose(1, activities.size());
    		return activities.get(choose-1);
      } 
}
